package br.com.klis.easycontrast.model;

import java.io.Serializable;

import br.com.klis.easycontrast.android.model.ColdContrastMode;
import br.com.klis.easycontrast.android.model.HotContrastMode;

public class ContrastSession implements Serializable{
	private static final long serialVersionUID = -5967788015787229251L;
	
	private int repetition = 0;
	private int time = 0;
	private ContrastMode hotContrastMode;
	private ContrastMode coldContrastMode;
	private ContrastMode contrastModeAtual;
	private Configuration configuration;
	

	public ContrastSession(Configuration config){
		this.configuration = config;
		hotContrastMode = new HotContrastMode(configuration.getHotTime(), configuration.getHotTimeSec());
		coldContrastMode = new ColdContrastMode(configuration.getColdTime(), configuration.getColdTimeSec());
	}
	
	public void initTime(){
		time = 0;
	}
	public void incrementTime(){
		time++;
		if (getTime() > 1){ //no primeiro tempo nao ha troca do modo atual, porque comeca com o modo setado no parametro startswith
			switchContrastModeAtual();
			if(isNewRepetition()){
				incrementRepetition(); // depois do primeiro tempo so incrementa a repeticao quando for uma nova
			}
		}else{
			incrementRepetition(); // na primeira vez sempre incrementa a repeticao porque ela inicia com zero
		}
	}
	public void initRepetition(){
		repetition = 0;
	}
	public void incrementRepetition(){
		repetition++;
	}
	public void initContrastMode(){
		if(getConfiguration().getStartsWith() == Constants.STARTS_WITH_HOT){
			setContrastModeAtual(getHotContrastMode());
		}else{
			setContrastModeAtual(getColdContrastMode());
		}
	}
	public void switchContrastModeAtual(){
		if(getContrastModeAtual() instanceof HotContrastMode){
			setContrastModeAtual(getColdContrastMode());
		}else if(getContrastModeAtual() instanceof ColdContrastMode){
			setContrastModeAtual(getHotContrastMode());
		}
	}
	
	public Configuration getConfiguration(){
		return this.configuration;
	}
	
	public int getTime() {
		return time;
	}

	public int getRepetition(){
		return this.repetition;
	}
	
	public ContrastMode getContrastModeAtual(){
		return this.contrastModeAtual;
	}
	private void setContrastModeAtual(ContrastMode contrastModeAtual) {
		this.contrastModeAtual = contrastModeAtual;
	}
	
	public int getTextColor(){
		return this.contrastModeAtual.getTextColor();
	}
	
	public ContrastMode getHotContrastMode() {
		return hotContrastMode;
	}

	public ContrastMode getColdContrastMode() {
		return coldContrastMode;
	}
	
	/**
	 * Checa se eh uma nova repeticao
	 * @return
	 */
	public boolean isNewRepetition(){
		if(getConfiguration().getStartsWith() == getContrastModeAtual().getId()){
			return true;
		}else{
			return false;
		}
	}
	public boolean isLastTimeOfSession(){
		if(getTime() == getConfiguration().getRepetitions()*2){
			return true;
		}else{
			return false;
		}
	}
	
}
